/**
 * Copyright 2009-2010 - YangJiandong(chunquedong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 你可以自由复制、传播本项目的下载包文件，但必须保持其完整性。
 * 我们不用对使用中的风险及由此造成的损失承担任何责任。
 * 详细情况请见《ChunMap许可协议》。

 * 想了解更多有关ChunMap的信息，请访问http://code.google.com/p/chunmap/
 */
package chunmap.model.coord;

import java.util.Iterator;


/**
 * @author chunquedong
 * 
 */
public class CompactedCoordinateArray implements CoordinateSeq {

	private final double[] x;
	private final double[] y;
	private int size = 0;

	public CompactedCoordinateArray(double[] x, double[] y) {
		super();
		size = x.length;
		if (size != y.length || size == 0) {
			throw new IllegalArgumentException("x[] not match y[]");
		}

		this.x = x;
		this.y = y;
	}
	
	public CompactedCoordinateArray(Position[] cs){
		size = cs.length;
        if (size == 0) throw new IllegalArgumentException();

        this.x = new double[size];
        this.y = new double[size];

        for (int i = 0; i < size;i++ )
        {
            x[i] = cs[i].getX();
            y[i] = cs[i].getY();
        }
	}

	@Override
	public Position getCoordinate(int index) {
		return new Coordinate2D(x[index], y[index]);
	}

	@Override
	public int size() {
		return size;
	}

	@Override
	public Iterator<Position> iterator() {
		return new Iterator<Position>() {
			int i = 0;

			@Override
			public boolean hasNext() {
				return i < size();
			}

			@Override
			public Position next() {
				return getCoordinate(i++);
			}

			@Override
			public void remove() {
				throw new UnsupportedOperationException();
			}
		};
	}

	@Override
	public CoordinateSeq format(PrecisionModel precision) {
		double[] x2 = formatDouble(x, precision);
		double[] y2 = formatDouble(y, precision);
		return new CompactedCoordinateArray(x2,y2);
	}

	private double[] formatDouble(double[] x1, PrecisionModel precision) {
		double[] x2 = new double[x1.length];
		for (int i = 0; i < size; i++) {
			x2[i] = precision.format(x1[i]);
		}
		return x2;
	}

	@Override
	public String toString() {
		return "CoordinateArray" + toMyString();
	}

//	@Override
//	public Iterable<Point> coordinates() {
//		return new Iterable<Point>() {
//			@Override
//			public Iterator<Point> iterator() {
//				return pointIterator();
//			};
//		};
//	}
//	
//	private Iterator<Point> pointIterator() {
//		return new Iterator<Point>() {
//			int i = 0;
//
//			@Override
//			public boolean hasNext() {
//				return i < size();
//			}
//
//			@Override
//			public Point next() {
//				return getCoordinate(i++);
//			}
//
//			@Override
//			public void remove() {
//				throw new UnsupportedOperationException();
//			}
//		};
//	}

	@Override
	public Position endPoint() {
		return new Coordinate2D(x[size-1], y[size-1]);
	}

	@Override
	public double[] getXa() {
		return x;
	}

	@Override
	public double[] getYa() {
		return y;
	}

	@Override
	public Position startPoint() {
		return new Coordinate2D(x[0], y[0]);
	}

	@Override
	public String toMyString() {
		StringBuilder str = new StringBuilder();
        str.append("(");
        for(int i=0;i<size;i++)
        {
            str.append(x[i] + " " + y[i] + ",");
        }
        str.delete(str.length() - 1, 1);
        str.append(")");
        return str.toString();
	}

	@Override
	public CoordinateSeq transform(Transform transf) {
		Position[] pointList = new Position[size];
		int i=0;
		for (Position p : this) {
			pointList[i]=transf.convert(p);
			i++;
		}
		return new CompactedCoordinateArray(pointList);
	}
	@Override
	public Object clone(){
		return new CompactedCoordinateArray((double[])x.clone(), (double[])y.clone());
	}
}
